﻿Imports KanBan.Pgd.Domain
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Services
Imports KanBan.Pgd.Presentation.Session

Public Class EditUserCompetency

    Private app As New PgdApp()

    Public Sub New()

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.InitializeView()

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Private Sub InitializeView()

        Me.LoadSelectableProperties()

    End Sub

    Private Sub LoadSelectableProperties()

        cmbCompetencies.DataSource = app.Competencies.GetAll()
        cmbCompetencies.ValueMember = "Id"
        cmbCompetencies.DisplayMember = "Name"

        cmbLevels.DataSource = app.KnowledgeLevels.GetValues()
        cmbLevels.ValueMember = "Arg1"
        cmbLevels.DisplayMember = "Arg2"

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Me.SaveOrUpdate()

    End Sub

    Private Sub SaveOrUpdate()

        Dim userId As Guid = ClientSession.UserCredentials.Id
        Dim user = app.Users.GetById(userId)

        Dim competencyId As Guid = CType(cmbCompetencies.SelectedValue, Guid)
        Dim competency = app.Competencies.GetById(competencyId)

        Dim level As KnowledgeLevel = CType(CInt(cmbLevels.SelectedValue), KnowledgeLevel)

        Dim uc As New UserCompetency(user, competency, level)
        app.Users.SaveOrUpdateCompetency(uc)

        Me.Close()

    End Sub

End Class